﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.IncomeVsOutflow.Search
{
    public class PresenterSearchIncomeVsOutflow
    {
        ISearchIncomeVsOutflow view;

        public PresenterSearchIncomeVsOutflow(ISearchIncomeVsOutflow view)
        {
            this.view = view;
        }

        public void SearchInOutflowAndIncomes()
        {
            try
            {
                decimal moneyIn = 0, moneyOut = 0;
                view.CollectsFinded = ManagerCollectControl.SearchIncomeByDates(view.InitialDate, view.EndDate, ref moneyIn);
                view.ExpensesFinded = ManagerExpenseControl.SearchOutFlowByDates(view.InitialDate, view.EndDate, ref moneyOut);
                view.TotalMoney = moneyIn - moneyOut;
            }
            catch (Exception ex)
            {
                view.CollectsFinded = null;
                view.ExpensesFinded = null;
                view.NotifyErrorMessage(ex.Message);
            }
        }

        public void SendToIncomeRead(Model.CollectControl collect)
        {
            if (collect is Model.WaterCollectControl)
                view.NavigateToIncomeRead((Model.WaterCollectControl)collect);

            if (collect is Model.OtherCollectControl)
                view.NavigateToIncomeRead((Model.OtherCollectControl)collect);

            if (collect is Model.FineCollectControl)
                view.NavigateToIncomeRead((Model.FineCollectControl)collect);
        }

        public void SendToOutflowRead(Model.ExpenseControl expenseControl)
        {
            if (expenseControl.IsWaterExpense)
            {
                Model.WaterPurchase expense = ManagerWaterPurchase.GetWaterPurchaseById(expenseControl.DateRegistry);
                view.NavigateToOutflowRead(expense);
            }
            else
            {
                Model.Expense expense = ManagerExpense.GetExpenseById(expenseControl.DateRegistry);
                view.NavigateToOutflowRead(expense);
            }
        }
    }
}
